<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/15
 * Time: 15:43
 */

namespace app\index\controller;


use app\admin\model\ClassType;
use app\admin\model\DriverType;
use app\admin\model\MakeUpExam;
use app\admin\model\OrderList;
use think\Controller;
use think\facade\Session;

class Makeupapi extends Base
{
    public function token()
    {
        $token = $this->request->token('__token__', 'sha1');
        return json(['code'=>1,'__token__'=>$token]);
    }
    public function makeClass(){
        //查询驾照类型
        $data['driverType']=DriverType::order('sort desc')->select();

        //查询科目类型
//        $data['classType']=ClassType::where('class_type','in',2)->select();
        $data['classType']=ClassType::where('FIND_IN_SET(2,class_type)')->select();

        return json(['code'=>1,'msg'=>'返回成功！','data'=>$data]);
    }
    public function examTypeData(){
        $post=input('post.');
        $data=MakeUpExam::where('exam_type',$post['d_id'])
            ->where('exam_subject',$post['c_id'])
            ->find();
        if($data){
            return json(['code'=>1,'msg'=>'返回成功！','data'=>$data]);
        }else{
            return json(['code'=>1,'msg'=>'返回成功！','data'=>$data]);
        }
    }
    public function phoneSend(){
        $post=input('post.');
        $send=new \app\index\controller\Send();
        $mobile=$post['phone'];
        $messageType='SMS_166376810';
        $code=rand(1000,9999);
        cookie('code',$code,300);
        $code=['code'=>$code];
        $res=$send->sms($mobile,$code,$messageType);
        return json($res);
    }
    //补考缴费
    public function markSubmit(){
        $post=input('post.');

        $validate = $this->validate($post,'app\index\validate\Makeup');
        if (true !== $validate){
            if($validate=='你已经提交过了'){
                $token = $this->request->token('__token__', 'sha1');
                return json(['code'=>1000,'msg'=>'你已提交过了!','__token__'=>$token]);
            }
            return json(['code'=>0,'msg'=>$validate]);
        }
//        $where['user_id']=Session::get('user_id');
//        $where['order_status']=1;
//        $where['test_type']=1;
//        $flag=OrderList::where($where)->find();
//        if(!$flag){
//            return json(['code'=>0,'msg'=>'您还没有参加报名']);
//        }

        $find=MakeUpExam::where('id',$post['d_id'])
            ->find();

        if(!$find){
            return json(['code'=>0,'msg'=>'商品不存在']);
        }
        if($post['code']!=cookie('code')){
            return json(['code'=>0,'msg'=>'验证码错误！']);
        }
        $data=[
            'order_no'=>$this->randomStr().time(),
            'user_id'=>Session::get('user_id'),
            'phone'=>$post['phone'],
            'true_name'=>$post['true_name'],
            'pay_money'=>$find['price'],
            'pay_type'=>0,
            'test_type'=>2,
            'sign_type'=>$find['exam_type'],
            'course_type'=>$find['exam_subject'],
            'create_time'=>time(),
            'id_card'=>$post['id_card'],
            'make_id'=>$post['d_id']
        ];
        $save=OrderList::insertGetId($data);
        if($save){
            cookie('code',null);
            $url='/index/wxpay/mackupPay?id='.$save;
            return json(['code'=>1,'msg'=>'提交成功！','jump_url'=>$url]);
        }
        return json(['code'=>0,'msg'=>'提交失败！']);
    }



}